<?php
namespace app\common\controller;

use think\auth\Auth;
use think\Loader;
use think\Cache;
use think\Controller;
use think\Db;
use think\Session;

/**
 * 后台公用基础控制器
 * Class AdminBase
 * @package app\common\controller
 */
class AdminBaseController extends Controller {
	
	protected $site_config;

    protected function _initialize() {
        parent::_initialize();
        $this->checkAuth();
        $this->getMenu();

        // 输出当前请求控制器（配合后台侧边菜单选中状态）
        $this->assign('controller', Loader::parseName($this->request->controller()));
		$this->assign('action', Loader::parseName($this->request->action()));
		
		// 配置
		if (Cache::has('site_config')) {
            $site_config = Cache::get('site_config');
        } else {
            $site_config = Db::name('system')->field('value')->where('name', 'site_config')->find();
            $site_config = unserialize($site_config['value']);
			cache('site_config', $site_config);
        }
		$this->assign('site_config',$site_config);
		$this->site_config = $site_config;
		// 判断登录状态
		$user = Db::name('user')->field('session_id')->where('id', session('admin_id'))->find();
		if($user['session_id'] != session_id()){
			Session::delete('admin_id');
        	Session::delete('admin_name');
			$this->success('该账号已在别处登录！', 'login/index');
		}
    }

    /**
     * 权限检查
     * @return bool
     */
    protected function checkAuth() {

        if (!Session::has('admin_id')) {
            $this->redirect('adminjcb/login/index');
        }

        $module     = $this->request->module();
        $controller = $this->request->controller();
        $action     = $this->request->action();

        // 排除权限
        $not_check = ['adminjcb/Index/index', 'adminjcb/Index/update', 'adminjcb/AuthGroup/getjson', 'adminjcb/System/clear'];

        if (!in_array($module . '/' . $controller . '/' . $action, $not_check)) {
            $auth     = new Auth();
            $admin_id = Session::get('admin_id');
            if (!$auth->check($module . '/' . $controller . '/' . $action, $admin_id) && $admin_id != 1 && $admin_id != 2) {
                $this->error('没有权限');
            }
        }
    }

    /**
     * 获取侧边栏菜单
     */
    protected function getMenu() {
        $menu     = [];
        $admin_id = Session::get('admin_id');
        $auth     = new Auth();

        $auth_rule_list = Db::name('auth_rule')->where('status', 1)->order(['sort' => 'asc', 'id' => 'asc'])->select();

        foreach ($auth_rule_list as $value) {
            if ($auth->check($value['name'], $admin_id) || $admin_id == 1 || $admin_id == 2) {
                $menu[] = $value;
            }
        }
        $menu = !empty($menu) ? array2tree($menu) : [];

        $this->assign('menu', $menu);
    }
}
